package cn.cao.domain.order.service;

import cn.cao.domain.order.adapter.port.IAlipayPort;
import cn.cao.domain.order.adapter.port.IProductPort;
import cn.cao.domain.order.adapter.repository.IOrderRepository;
import cn.cao.domain.order.model.aggregate.CreateOrderAggregate;
import cn.cao.domain.order.model.entity.PayOrderEntity;
import cn.cao.domain.order.model.valobj.OrderStatusVO;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author caoyi
 * @description:
 * @Date 2025/7/11 22:02
 */
@Slf4j
@Service
public class OrderService extends AbstractOrderService{

    protected final IAlipayPort alipayPort;


    public OrderService(IOrderRepository repository, IProductPort port, IAlipayPort alipayPort) {
        super(repository, port);
        this.alipayPort = alipayPort;
    }

    @Override
    protected PayOrderEntity doPrepayOrder(String userId, String productId, String productName, String orderId, BigDecimal totalAmount) throws AlipayApiException {

        String form=alipayPort.getPayForm(userId,productId,productName,orderId,totalAmount);

        PayOrderEntity payOrderEntity = new PayOrderEntity();
        payOrderEntity.setOrderId(orderId);
        payOrderEntity.setPayUrl(form);
        payOrderEntity.setOrderStatus(OrderStatusVO.PAY_WAIT);

        repository.updateOrderPayInfo(payOrderEntity);

        return payOrderEntity;
    }

    @Override
    protected void doSaveOrder(CreateOrderAggregate orderAggregate) {
        //数据库变动，调用仓储
        repository.doSaveOrder(orderAggregate);
    }

    @Override
    public void changeOrderPaySuccess(String orderId) {
        repository.changeOrderPaySuccess(orderId);
    }

    @Override
    public List<String> queryNoPayNotifyOrder() {
        return repository.queryNoPayNotifyOrder();
    }

    @Override
    public List<String> queryTimeoutCloseOrderList() {
        return repository.queryTimeoutCloseOrderList();
    }

    @Override
    public boolean changeOrderClose(String orderId) {
        return repository.changeOrderClose();
    }
}
